import TauSimpleSelect from '@/components/TauSimpleSelect';
import { DictItemSelect } from '@/components/biz';
import * as BASE from '@/stores/definitions-base';
import { Switch } from '@alifd/next';
import { t } from '@lingui/macro';
import React from 'react';

const {
  DATETIME_TEXT,
  DATE_TEXT,
  TIME_TEXT,
  EMPTY_TEXT,
  ENUMS: ENUMSBase,
  FieldsGrid: FieldGridBase,
  TitleGrid,
  column: columnBase,
  toSelectDataSource,
} = BASE;
export { DATETIME_TEXT, DATE_TEXT, EMPTY_TEXT, TIME_TEXT, TitleGrid, toSelectDataSource };

/**
 * @param {*} path        属性path
 * @param {*} props       column属性
 * @returns column定义
 */
export function column(...args) {
  return columnBase(model, ...args);
}

/**
 * 显示一组表单字段
 * ```js
 * {
 *   fields: string[],    // 字段名称，可以使用.来指定嵌套对象属性，例如order.busOrder.shift
 *   columns: number,     // 列数，默认为4,最大为12,最小为1
 *   model: object,       // 字段定义数据模型的根对象，默认不指定，仅在使用子对象作为根模型时设置
 *   overrideModel: object, // 重载字段定义数据模型
 *   mode: string,        // 表单模式，默认为edit，可选view
 *   itemLayout: object,  // FormItem布局配置，默认为{}
 * }
 * ```
 *
 * @param {object} param0
 * @returns
 */
export function FieldsGrid(props) {
  return <FieldGridBase model={model} {...props} />;
}

//----------------------------------------------------------------------
export const ENUMS = {
  ...ENUMSBase,
  state: { // 启用状态
    true: t`启用`,
    false: t`禁用`,
  },
  channelType: { // 酒店类型
    0: t`酒店`,
    1: t`旅行社`,
  },
  bindAccountType: { // 分账接收方类型
    MERCHANT_ID: t`公司商户号`,
    PERSONAL_OPENID: t`个人微信帐户`,
  },
  acceptorType: { // 分账接收方类型
    0: t`平台`,
    1: t`渠道`,
  },
  // ：D+0、D+1、D+2、D+7、D+30(加几值为几，例：D+0 值为0)
  period: {
    0: t`D+0`,
    1: t`D+1`,
    2: t`D+2`,
    7: t`D+7`,
    30: t`D+30`,
  },
  independentAccountType: {
    0: '按比例分账',
    1: '按固定金额分账',
  },
};

/**
 * 模型定义：
 * 1. 普通文本仅需要定义label
 * 2. 需要通过枚举转换的值，定义text; 当使用renderer返回组件时，text属性无效，需要通过组件的renderPreview来指定预览时的绘制方式
 * 3. 通用的ui属性需要定制时，定义ui; 特定模式(view, edit, add)下的ui，添加后缀定义为ui_<mode>，例如：ui_view, ui_edit, ui_add, ui_table
 * 4. ui可配置属性包括:
 *    renderer: 内容渲染函数, Form表单中的原型为：(value: any, formData: any, fieldDefinition: any) => ReactNode; Table Column中的原型为: (value: any, columnIndex: number, rowData: any) => ReactNode;
 *    itemProps: 表单Form.Item的属性，可以是对象，或函数({ isPreview: boolean, mode: string, value: any, formData: any, ...otherDefinitions}) => void;
 *    span: Grid Cell占用的列数, 默认为1
 *
 * 【注】获取接口的model定义后，vscode中通过正则替换：^(\s+)"label": "([^']+)"  ===> $1label: t`$2`
 */
export const model = {
  start: {
    label: t`出发地`,
  },
  end: {
    label: t`目的地`,
  },
  price: {
    label: t`价格汇总`,
    RMB: {
      label: t`价格`,
    },
  },
  merchantCount: {
    label: t`商户数`,
  },
  ccOrderCount: {
    label: t`包车订单数`,
  },
  id: {
    label: t`编号`,
    ui: {
      itemProps: { isPreview: true },
    },
  },
  name: {
    label: t`渠道名称`,
    ui: {
      itemProps: { required: true },
    },
  },
  appointCompanies: {
    label: t`指定承运商`,
    ui: {
      renderer: (v) => <TauSimpleSelect data={rootStore.channelStore.company} value={v} multiple />,
      span: 2,
    },
  },
  type: {
    label: t`类型`,
    ui: {
      renderer: (v) => <TauSimpleSelect data={rootStore.channelStore.channelTypes} value={v} />,
      itemProps: { required: true },
    },
  },
  typeText: {
    label: t`类型名称`,
  },
  creditCode: {
    label: t`信用代码`,
  },
  mobile: {
    label: t`联系电话`,
    ui: {
      itemProps: { required: true, format: 'tel' },
    },
  },
  address: {
    label: t`地址`,
    ui: {
      itemProps: { required: true },
    },
  },
  state: {
    label: t`启用`,
    ui_table: {
      renderer: (v, i, row) => (<Switch
        checked={v}
        size="small"
        autoWidth
        checkedChildren="启用"
        unCheckedChildren="禁用"
        onClick={() => rootStore.channelStore.updateState(row)}
      />),
    },
  },
  independentAccount: {
    label: t`分账设置信息`,
    period: {
      label: t`分账周期`,
      text: (v) => `T+${v}`,
      ui: {
        renderer: (v) => <TauSimpleSelect data={toSelectDataSource(ENUMS.period)} value={v} />,
        itemProps: { required: true },
      },
    },
    suspensionOfDistribution: {
      label: t`暂停分账`,
      ui: {
        renderer: (v) => <Switch checkedChildren="是" unCheckedChildren="否" autoWidth checked={v} size="small" renderPreview={(v0) => `${v0 ? '暂停' : '可分账'}`} />,
      },
    },
    settlementOfDriverCharges: {
      label: t`平台结算商户佣金`,
      ui: {
        renderer: (v) => <Switch checked={v} size="small" renderPreview={(v0) => `${v0 ? '是' : '否'}`} />,
      },
    },
    collectionAccountInformation: {
      label: t`收款账户信息`,
      appId: {
        label: t`公众号appId`,
      },
      accountIdentification: {
        label: t`账户标识`,
      },
      name: {
        label: t`名字`,
      },
      type: {
        label: t`分账接收方类型`,
        text: (v) => ENUMS.bindAccountType[v],
        ui: {
          renderer: (v) => <TauSimpleSelect data={toSelectDataSource(ENUMS.bindAccountType)} value={v} />,
          itemProps: { required: true },
        },
      },
    },
    acceptorType: {
      label: t`分账对象`,
    },
    independentAccountType: {
      label: t`分账类型`,
      ui: {
        itemProps: { required: true },
      },
    },
    independentAccountValue: {
      label: t`分账金额`,
      RMB: {
        label: t`分账金额`,
        ui: {
          itemProps: { required: true },
        },
      },
    },
    viewAcceptorType: {
      label: t`分账对象(回显)`,
    },
    viewIndependentAccountType: {
      label: t`分账收款类型(回显)`,
    },
    viewIndependentAccountValue: {
      label: t`分账金额`,
      RMB: {
        label: t`分账金额(回显)`,
      },
    },
  },
  principalInfo: {
    label: t`负责人信息`,
    principal: {
      label: t`负责人username`,
    },
    principalName: {
      label: t`负责人姓名`,
      ui: {
        itemProps: { required: true },
      },
    },
    principalMobile: {
      label: t`负责人电话`,
      ui: {
        itemProps: { required: true, format: 'tel' },
      },
    },
    certificateType: {
      label: t`证件类型`,
      ui: {
        renderer: (v) => <DictItemSelect dictId="directorIdType" value={v} />,
      },
    },
    idNumber: {
      label: t`证件号`,
    },
  },
  bankAccounts: {
    label: t`开户行信息`,
    '{key}': {
      bank: {
        label: t`[key]开户银行`,
        ui: {
          renderer: (v) => <DictItemSelect dictId="bank" value={v} renderPreview={(itm) => EMPTY_TEXT(itm?.name)} />,
        },
      },
      accountName: {
        label: t`[key]账户名称`,
        ui: {
          span: 2,
        },
      },
      account: {
        label: t`[key]银行账号`,
      },
    },
  },
  attachments: [
    {
      label: t`附件`,
      title: {
        label: t`附件标题`,
      },
      description: {
        label: t`附件说明`,
      },
      binaryId: [
        {
          label: t`附件id`,
        },
      ],
    },
  ],
  remarks: {
    label: t`备注`,
  },
  crBy: {
    label: t`创建者`,
  },
  upBy: {
    label: t`最后修改者`,
  },
  crDate: {
    label: t`创建时间`,
    text: DATETIME_TEXT,
  },
  upDate: {
    label: t`更新时间`,
    text: DATETIME_TEXT,
  },
  tenantId: {
    label: t`租户ID`,
  },
};
